package com.manager.listener;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.manager.base.BaseService;
import com.manager.base.impl.BaseServiceImpl;
import com.manager.util.CONSTANT;
import com.manager.util.Log;

public class InitApplicationContextListener implements ServletContextListener {

	public void contextInitialized(ServletContextEvent servletContextEvent) {
		try{
			ServletContext servletContext = servletContextEvent.getServletContext();
			ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
			
			//读取config.properties配置文件中的信息，并写入到CONSTANT类中
			Properties config = new Properties();
			config.load(servletContext.getResourceAsStream("/WEB-INF/classes/config.properties"));
			
			CONSTANT.RESET_PASSWORD = config.getProperty("resetPassword");
			CONSTANT.PAGE_SIZE = Integer.parseInt(config.getProperty("pageSize"));
			
			//邮件服务器设置
			CONSTANT.MAIL_SERVER_CONFIG.HOST = config.getProperty("mail.server.host");
			CONSTANT.MAIL_SERVER_CONFIG.PORT = config.getProperty("mail.server.port");
			CONSTANT.MAIL_SERVER_CONFIG.VALIDATE = Boolean.parseBoolean(config.getProperty("mail.server.validate"));
			CONSTANT.MAIL_SERVER_CONFIG.USERNAME = config.getProperty("mail.server.username");
			CONSTANT.MAIL_SERVER_CONFIG.PASSWORD = config.getProperty("mail.server.password");
			CONSTANT.MAIL_SERVER_CONFIG.FROM_ADDRESS = config.getProperty("mail.server.fromAddress");
			
			//初始化项目名称，在每个页面的title中会用到
			servletContext.setAttribute("projectName", config.getProperty("projectName"));
			servletContext.setAttribute("projectNameDetail", config.getProperty("projectNameDetail"));
			servletContext.setAttribute("copyright", config.getProperty("copyright"));
			
			/*
			 * 项目根路径
			 * 查看你的tomcat配置
			 * (1)打开%CATALINA_HOME%/conf/server.xml
			 * 	  假如有如下配置：<Context path="" ... ...></Context>，则BASE_PATH = ""
			 *    假如有如下配置：<Context path="/manager" ... ...></Context>，则BASE_PATH = "/manager"
			 *  注意：
			 *  (1)******只推荐使用这两种方式******
			 *  (2)这个路径在某些页面中会直接引用，主要是为了方便
			 */
			servletContext.setAttribute("basePath", config.getProperty("basePath"));
			
			//设置公用服务以供任何地方可以随时调用
			BaseService baseService = (BaseServiceImpl)applicationContext.getBean("baseServiceImpl");
			servletContext.setAttribute("baseService", baseService);
			
			//初始化配置皮肤文件
			String skinColor =  config.getProperty("skinColor");
			Map<String, String> m = new HashMap<String, String>();
			m.put("top", "base/css/" + skinColor + "/top.css");
			m.put("bottom", "base/css/" + skinColor + "/bottom.css");
			m.put("left", "base/css/" + skinColor + "/left.css");
			m.put("form", "base/css/" + skinColor + "/form.css");
			m.put("displaytag", "base/css/" + skinColor + "/displaytag.css");
			m.put("other", "base/css/" + skinColor + "/other.css");
			
			servletContext.setAttribute("skin", m);
			
			//设置一个引用指向servletContext，以供任何地方可以随时调用
			CONSTANT.SERVLET_CONTEXT = servletContext;
			
			Log.info("****** Succeed to init ServletContext ******<----------", InitApplicationContextListener.class);
		} catch (Exception e){
			Log.info("?????? failed to init ServletContext ??????<----------", InitApplicationContextListener.class);
		}
	}

	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		
	}
}
